package com.ruyuan.user.server.domain.converter;

import com.ruyuan.common.model.Features;
import com.ruyuan.user.server.domain.model.Role;
import com.ruyuan.user.server.repository.bean.RoleDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author xx
 */
@Component
public class RoleConverter {

    public RoleDO convertModelToDO(Role role) {
        if (role == null) {
            return null;
        }
        RoleDO roleDO = new RoleDO();
        BeanUtils.copyProperties(role, roleDO);
        roleDO.setIsDeleted("n");
        if (role.getIsDeleted() != null && role.getIsDeleted()) {
            roleDO.setIsDeleted("y");
        }
        if (role.getFeatures() != null) {
            roleDO.setFeatures(role.getFeatures().toString());
        }
        roleDO.setGmtModified(new Date());
        roleDO.setGmtCreate(new Date());
        roleDO.setCreator(role.getModifier());
        return roleDO;
    }

    public Role convertDOToModel(RoleDO roleDO) {
        if (roleDO == null) {
            return null;
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDO, role);
        role.setIsDeleted("y".equals(roleDO.getIsDeleted()));
        if (StringUtils.isNotBlank(roleDO.getFeatures())) {
            role.setFeatures(Features.of(roleDO.getFeatures()));
        }
        return role;
    }

}
